package com.tuoyu.peopletest.presenter;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;

import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.utils.SCLogUtils;


/**
 * 加载动画
 */
public class LoadUIPresenter {

    private final static String TAG = "LoadUIPresenter";
    public final static int NOMAL = 0;
    public int type = NOMAL;
    public View vLoadUI;
    public ViewGroup vMainView;
    private boolean isStartAnim;
    private boolean isShowLoading;
    private View ivLoading;
    private ObjectAnimator anim;

    public LoadUIPresenter() {

    }

    public static LoadUIPresenter get() {

        return new LoadUIPresenter();
    }


    public LoadUIPresenter setMainView(ViewGroup mainView) {
        this.vMainView = mainView;
        return this;
    }

    /**
     * 设置类型
     *
     * @param type
     */
    public void setType(int type) {
        this.type = type;
    }

    public void initNomalUI() {
        switch (type) {
            case NOMAL:
                vLoadUI = View.inflate(vMainView.getContext(), R.layout.view_loading, null);
                ivLoading = vLoadUI.findViewById(R.id.v_loading);

                vMainView.addView(vLoadUI);
                vLoadUI.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
                vLoadUI.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                vLoadUI.setVisibility(View.INVISIBLE);
                break;
        }
    }


    public void showLoading() {
        if (vMainView == null) {
            throw new NullPointerException("vMainView 不能为空");
        }

        if (isShowLoading) {
            return;
        }

        if (isStartAnim) {
            return;
        }
        SCLogUtils.e( TAG,"showLoading");

        ivLoading.clearAnimation();
        vLoadUI.setVisibility(View.VISIBLE);
        isShowLoading = true;

        anim = ObjectAnimator.ofFloat(ivLoading,"rotation",360*3*60*1.5f);
        //3分钟
        anim.setDuration(3*60*1000);
        anim.setInterpolator(new LinearInterpolator());
        anim.start();

    }


    public boolean isShowLoading(){
        return isShowLoading;
    }

    public void hideLoading() {
        if(anim!=null){
            anim.cancel();
        }

        //一个渐变动画
        vLoadUI.animate()
                .alphaBy(0)
                .setDuration(200)
                .setListener(new AnimatorListenerAdapter() {

                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        isStartAnim = true;
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        ivLoading.clearAnimation();
                        isStartAnim = false;
                        isShowLoading = false;
                        vLoadUI.setVisibility(View.INVISIBLE);
                        //旋转角度还原
                        vLoadUI.setRotation(0);
                    }
                }).start();
    }

}
